package cn.org.xuanzp.nty.server;

import cn.org.xuanzp.nty.server.channel.NettyServerChannelInitializer;
import cn.org.xuanzp.nty.server.handler.CommandHandler;
import cn.org.xuanzp.nty.server.handler.DoNothingCommandHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @Classname NettyServer
 * @Description netty 服务启动类
 * @Version 1.0.0
 * @Date 2024/9/22 16:34
 * @Created by xuanzp
 */
public class NettyServer {

    private ServerBootstrap bootstrap ;
    private EventLoopGroup mainEventLoopGroup;
    private EventLoopGroup otherEventLoopGroup;
    private ChannelInitializer<Channel> channelInitializer;
    private int port;

    public NettyServer(int port,int mainThreads,int nThreads) {
        this.port = port;
        this.mainEventLoopGroup=mainThreads <= 0?new NioEventLoopGroup():new NioEventLoopGroup(mainThreads);
        this.otherEventLoopGroup=nThreads <= 0?new NioEventLoopGroup():new NioEventLoopGroup(nThreads);
        bootstrap = new ServerBootstrap();
        channelInitializer =new NettyServerChannelInitializer(new DoNothingCommandHandler());
    }



    public NettyServer(int port,int mainThreads,int nThreads,CommandHandler commandHandler) {
        this.port = port;
        this.mainEventLoopGroup=mainThreads <= 0?new NioEventLoopGroup():new NioEventLoopGroup(mainThreads);
        this.otherEventLoopGroup=nThreads <= 0?new NioEventLoopGroup():new NioEventLoopGroup(nThreads);
        bootstrap = new ServerBootstrap();
        channelInitializer =new NettyServerChannelInitializer(commandHandler);
    }

    public NettyServer(int port,int nThreads,CommandHandler commandHandler) {
        this.port = port;
        this.mainEventLoopGroup=new NioEventLoopGroup(1);
        this.otherEventLoopGroup=nThreads <= 0?new NioEventLoopGroup():new NioEventLoopGroup(nThreads);
        bootstrap = new ServerBootstrap();
        channelInitializer =new NettyServerChannelInitializer(commandHandler);
    }

    public NettyServer(int port,CommandHandler commandHandler) {
        this.port = port;
        this.mainEventLoopGroup=new NioEventLoopGroup(1);
        this.otherEventLoopGroup=new NioEventLoopGroup(5);
        bootstrap = new ServerBootstrap();
        channelInitializer =new NettyServerChannelInitializer(commandHandler);
    }

    public void start() throws InterruptedException {
        bootstrap.group(mainEventLoopGroup,otherEventLoopGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(channelInitializer);

        ChannelFuture channelFuture = bootstrap.bind(port).sync();
        if (channelFuture.isSuccess()) {
            System.out.println("服务端启动成功");
        } else {
            System.out.println("服务端启动失败");
            channelFuture.cause().printStackTrace();
            close();
        }
    }

    public void close() {
        mainEventLoopGroup.shutdownGracefully();
        otherEventLoopGroup.shutdownGracefully();
    }
}
